import utils from '../../core/utils';

export default class cacheInfo {
  constructor(comicId: string, comicName: string, cover: string) {
    this._cid = comicId;
    this._title = comicName;
    this._cover = cover;
  }

  private _currentEp: epItem;

  public get currentEp() {
    return this._currentEp;
  }

  public set currentEp(currentEp: epItem) {
    this._currentEp = currentEp;
  }

  public updateEp(epId: string, epTitle: string, notifyId:number, success: boolean, total: number): epItem {
    let ep = this._eps.find((i) => i.epId == epId);
    if (utils.isEmpty(ep)) {
      ep = new epItem(epId, epTitle, 0, 0, total);
      this._eps.push(ep);
    }
    if (success) {
      ep.success++;
      utils.updateNotify(notifyId, '下载中', `共 ${total} / 成功 ${ep.success} /失败 ${ep.failed}`);
    } else {
      ep.failed++;
      utils.updateNotify(notifyId, '下载中', `共 ${total} / 成功 ${ep.success} /失败 ${ep.failed}`);
    }
    if (ep.success + ep.failed == ep.total)
      utils.updateNotify(notifyId, '下载完成', `共 ${total} / 成功 ${ep.success} /失败 ${ep.failed}`);
    return ep;
  }

  private _eps: epItem[] = [];

  public get eps() {
    return this._eps;
  }

  public addEp(epId: string, title: string, success: number, failed: number, total?: number) {
    let item = this._eps.find((i) => i.epId == epId);
    if (utils.isEmpty(item)) {
      if(!total) {
        total = success + failed;
      }
      let obj = new epItem(epId, title, success, failed, total)
      this._eps.push(obj);
    } else {
      item.update(success, failed);
    }
  }

  private _cid: string;

  public get cid() {
    return this._cid;
  }

  private _title: string;

  public get title() {
    return this._title;
  }

  private _cover: string;

  public get cover() {
    return this._cover;
  }
}

export class epItem {
  constructor(epId: string, title: string, success: number, failed: number, total: number) {
    this._epId = epId;
    this._epTitle = title;
    this._success = success;
    this._failed = failed;
    this._total = total;
  }

  public update(successValue: number, failedValue: number) {
    this._success = successValue;
    this._failed = failedValue;
  }

  public addImg(img: string) {
    this._imgs.push(img);
  }

  private _epId: string;

  public get epId() {
    return this._epId;
  }

  private _epTitle: string;

  public get epTitle() {
    return this._epTitle;
  }

  private _success: number;

  public get success() {
    return this._success;
  }

  public set success(success: number) {
    this._success = success;
  }

  private _failed: number;

  public get failed() {
    return this._failed;
  }

  public set failed(failed: number) {
    this._failed = failed;
  }

  private _total: number;

  public get total() {
    return this._total;
  }

  private _imgs: string[] = [];

  public get imgs() {
    return this._imgs;
  }
}